<?php
/**
 * Created by PhpStorm.
 * User: maqun
 * Date: 2016/2/29
 * Time: 12:35
 * 问题管理
 */
class ProblemAction extends BaseAction{

    protected $page_size = 10;

    public function _initialize(){
        self::$action = 2;
        parent::_initialize();
    }

    #问题列表
    public function index(){

        vendor('Page');
        $condition = I('get.condition');
        $where = !empty($condition) ? " locate(title,'".$condition."')" : '';

        $count = D('Problem')->where($where)->count();

        $page  = new Page($count,$this->page_size);

        $list = D('Problem')->where($where)
            ->order('in_date DESC')
            ->limit($page->firstRow.','.$page->listRows)
            ->select();

        $this->assign('condition',$condition);
        $this->assign('page',$page);
        $this->assign('list',$list);

        $this->display('index');
    }
    #添加显示页面
    public function add(){

        $this->display('add');
    }
    #修改显示页面
    public function edit(){
        $get = $this->_get('pid');
        $content = D('Problem')->where("problem_id = $get")->select();

        $this->assign('content',$content[0]);
        $this->display('edit');

    }
    #删除问题
    public function delete(){
        $get = $this->_get('pid');

        $News = D('Problem')->where("problem_id = $get")->delete();
        if($News){
            header("Location: /admin.php/Problem/index");
        }
        
    }
    #重判题目
    public function rejudge(){

        $data = $this->_post();
        if(empty($data)){
            $this->display('rejudge');
            exit;
        }
        //提交处理
        $res = array('status'=>false,'info'=>'操作失败');

        $model = D('Problem');
        if (isset($data['problem-id'])){
            $res = $model->rejudgeProblemByPid($data['problem-id']);

        }else if (isset($data['solution-id'])){
            $res = $model->rejudgeProblemBySid($data['solution-id']);
        }
        $this->error($res['info']);
        exit;

    }
    #添加修改数据保存
    public function save(){
        $post = $this->_post();

        if(!empty($post)){
            $post["in_date"] = date("Y-m-d H:i:s");

            if(!empty($post['id'])){
                $News = D('Problem')->where("problem_id = $post[id]")->save($post);
            }else{
                $sample_input = $post['sample_input'];
                $sample_output = $post['sample_output'];
                $test_input = $post['test_input'];
                $test_output = $post['test_output'];

                $OJ_DATA = C('OJ_DATA');
                $pid = D('Problem')->add($post);
                $basedirs = "$OJ_DATA/$pid";
                
                mkdir ( $basedirs );
                if(strlen($sample_output)&&!strlen($sample_input)){
                    $sample_input="0";
                }

                if(strlen($sample_input)){
                    $this->mkdata($pid,"sample.in",$sample_input,$OJ_DATA);
                }

                if(strlen($sample_output)){
                    $this->mkdata($pid,"sample.out",$sample_output,$OJ_DATA);
                }

                if(strlen($test_output)&&!strlen($test_input)){
                    $test_input="0";
                }

                if(strlen($test_input)){
                    $this->mkdata($pid,"test.in",$test_input,$OJ_DATA);
                }

                if(strlen($test_output)){
                    $this->mkdata($pid,"test.out",$test_output,$OJ_DATA);
                }
            }
        }
        header("Location: index");
    }
    public function mkdata($pid,$filename,$input,$OJ_DATA){

        // $OJ_DATA = C('OJ_DATA');
        $basedir = "$OJ_DATA/$pid";
        
        $fp = @fopen ( $basedir . "/$filename", "w" );
        if($fp){
            fputs ( $fp, preg_replace ( "(\r\n)", "\n", $input ) );
            fclose ( $fp );
        }else{
            echo "Error while opening".$basedir . "/$filename ,try [chgrp -R www-data $OJ_DATA] and [chmod -R 771 $OJ_DATA ] ";
            
        }
    }



}